package cn.iocoder.yudao.module.vote.controller.admin.voteuserresultdetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.voteuserresultdetail.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresultdetail.VoteUserResultDetailDO;
import cn.iocoder.yudao.module.vote.convert.voteuserresultdetail.VoteUserResultDetailConvert;
import cn.iocoder.yudao.module.vote.service.voteuserresultdetail.VoteUserResultDetailService;

@Api(tags = "管理后台 - 投票结果信息详情")
@RestController
@RequestMapping("/vote/user-result-detail")
@Validated
public class VoteUserResultDetailController {

    @Resource
    private VoteUserResultDetailService userResultDetailService;

    @PostMapping("/create")
    @ApiOperation("创建投票结果信息详情")
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:create')")
    public CommonResult<Long> createUserResultDetail(@Valid @RequestBody VoteUserResultDetailCreateReqVO createReqVO) {
        return success(userResultDetailService.createUserResultDetail(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新投票结果信息详情")
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:update')")
    public CommonResult<Boolean> updateUserResultDetail(@Valid @RequestBody VoteUserResultDetailUpdateReqVO updateReqVO) {
        userResultDetailService.updateUserResultDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除投票结果信息详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:delete')")
    public CommonResult<Boolean> deleteUserResultDetail(@RequestParam("id") Long id) {
        userResultDetailService.deleteUserResultDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投票结果信息详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:query')")
    public CommonResult<VoteUserResultDetailRespVO> getUserResultDetail(@RequestParam("id") Long id) {
        VoteUserResultDetailDO userResultDetail = userResultDetailService.getUserResultDetail(id);
        return success(VoteUserResultDetailConvert.INSTANCE.convert(userResultDetail));
    }

    @GetMapping("/list")
    @ApiOperation("获得投票结果信息详情列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:query')")
    public CommonResult<List<VoteUserResultDetailRespVO>> getUserResultDetailList(@RequestParam("ids") Collection<Long> ids) {
        List<VoteUserResultDetailDO> list = userResultDetailService.getUserResultDetailList(ids);
        return success(VoteUserResultDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得投票结果信息详情分页")
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:query')")
    public CommonResult<PageResult<VoteUserResultDetailRespVO>> getUserResultDetailPage(@Valid VoteUserResultDetailPageReqVO pageVO) {
        PageResult<VoteUserResultDetailDO> pageResult = userResultDetailService.getUserResultDetailPage(pageVO);
        return success(VoteUserResultDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出投票结果信息详情 Excel")
    @PreAuthorize("@ss.hasPermission('vote:user-result-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportUserResultDetailExcel(@Valid VoteUserResultDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteUserResultDetailDO> list = userResultDetailService.getUserResultDetailList(exportReqVO);
        // 导出 Excel
        List<VoteUserResultDetailExcelVO> datas = VoteUserResultDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投票结果信息详情.xls", "数据", VoteUserResultDetailExcelVO.class, datas);
    }

}
